package com.shwread.android.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.shwread.android.qysw.activity.ReadingBookActivity;
import com.shwread.android.qysw10000204.R;

import logic.util.logutil.LogUtil;

public class PopWindowBright
        extends
        ReadingPopWindow
        implements
        OnClickListener,
        OnSeekBarChangeListener
{
	private TextView                tvBrightValue;
	private Button                  btnBrightDown;
	private Button                  btnBrightUp;
	private SeekBar                 sbBright;
	
	private static ReadingPopWindow instance;
	
	public static ReadingPopWindow getInstance(Context context) {
		if (instance == null) instance = new PopWindowBright(context);
		instance.context = context;
		return instance;
	}
	
	private PopWindowBright(Context context) {
		View contentView = LayoutInflater.from(context).inflate(R.layout.menu_pop_bright, null);
		init(context, contentView);
		ReadingBookActivity readerActivity = (ReadingBookActivity)context;
		int progresss = readerActivity.myFBReaderApp.brightOption.getValue();
		sbBright.setProgress(progresss);
	}
	
	@Override
	protected void addListener() {
		btnBrightDown.setOnClickListener(this);
		btnBrightUp.setOnClickListener(this);
		sbBright.setOnSeekBarChangeListener(this);
	}
	
	@Override
	protected void initView(View parentView) {
		tvBrightValue = (TextView) parentView.findViewById(R.id.menu_pop_bright_value_tv);
		btnBrightDown = (Button) parentView.findViewById(R.id.menu_pop_bright_left_btn);
		btnBrightUp = (Button) parentView.findViewById(R.id.menu_pop_bright_right_btn);
		sbBright = (SeekBar) parentView.findViewById(R.id.menu_pop_bright_sb);
	}
	
	@Override
	public void onClick(View v) {
		int progress = sbBright.getProgress();
		switch (v.getId()) {
			case R.id.menu_pop_bright_left_btn:
				progress = progress - 10 <= 0 ? 0 : progress - 10;
				break;
			case R.id.menu_pop_bright_right_btn:
				progress = progress + 10 >= 100 ? 100 : progress + 10;
				break;
		}
		sbBright.setProgress(progress);
	}
	
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		btnBrightDown.setEnabled(progress != 0);
		btnBrightUp.setEnabled(progress != 100);
		tvBrightValue.setText(progress + "");
		LogUtil.d("luhuhai", tvBrightValue.getText().toString());
		ReadingBookActivity readerActivity = (ReadingBookActivity)context;
		readerActivity.myFBReaderApp.brightOption.setValue(progress);
		setBrightness(progress);
	}
	
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}
	
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}
	
	public void setBrightness(int up) {
		WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
		lp.screenBrightness = (55 + up * 2) / 255.0f;
		((Activity) context).getWindow().setAttributes(lp);
	}
}
